﻿//----------------------------------------------------------------
//CodeArt.
//
//文件描述:
//
//创 建 人: jianyi
//Mail:jianyi0115@163.com,
//Blog:http://www.cnblogs.com/jianyi0115
//创建日期: 2014-6-1
//
//修订记录: 
//
//----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;


namespace CodeArt.SharePoint.CamlQuery
{
    // <In>
    //  <FieldRef Name="MeetingStatus" />
    //  <Values>
    //    <Value Type="Text">进行中</Value>
    //    <Value Type="Text">已批准</Value>
    //    <Value Type="Text">已结束</Value>
    //  </Values>
    //</In>
    class InExpression : CamlExpression
    {
        private object[] options;
        private string valueType = "Text"; //Integer

        public InExpression(string field, bool isInternalName, object[] options, string valueType)
        {
            FieldName = field;
            IsInternalName = isInternalName;
            this.options = options;
            this.valueType = valueType;
        }

        public readonly string FieldName;

        public readonly bool IsInternalName;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="parentNode"></param>
        public override void ToCaml(ICamlBuildProvider provider, System.Xml.XmlNode parentNode)
        {
            XmlElement node = parentNode.OwnerDocument.CreateElement("In");
            parentNode.AppendChild(node);

            XmlNode fieldNode = parentNode.OwnerDocument.CreateElement("FieldRef");
            node.AppendChild(fieldNode);

            base.ApppendAttribute(fieldNode, "Name",
               base.GetFieldInternalName(provider, this.FieldName, this.IsInternalName));

            if (this.valueType == SPFieldType.Lookup.ToString())
            {
                base.ApppendAttribute(fieldNode, "LookupId", "TRUE");
            }

            XmlNode valuesNode = parentNode.OwnerDocument.CreateElement("Values");
            node.AppendChild(valuesNode);

            foreach (var op in this.options)
            {
                XmlNode vNode = parentNode.OwnerDocument.CreateElement("Value");
                vNode.InnerText = op.ToString();
                valuesNode.AppendChild(vNode);

                base.ApppendAttribute(vNode, "Type", this.valueType);           
            }
        }        
    }     
}
